package org.example.dingtalkintegrationservice.pojo.form;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pc
 */
public class FormInstance {
    public String processId;
    public String classCode;
    public String processInstanceId;
    public String flowId;
    public String originatorUserId;
    public String title;
    public List<FieldInstance> fieldInstances=new ArrayList<>();
    @JsonTypeInfo(
            use = JsonTypeInfo.Id.NAME, // 使用字段名称来确定子类
            include = JsonTypeInfo.As.PROPERTY, // 字段在JSON中存储为属性
            property = "type" // 字段名称
    )
    @JsonSubTypes({
            @JsonSubTypes.Type(value = TextFieldInstance.class, name = "TextFieldInstance"),
            @JsonSubTypes.Type(value = TableFieldInstance.class, name = "TableFieldInstance")
    })
    public static class FieldInstance {
        public String name;
        public String type;
    }
    public static class TextFieldInstance extends FieldInstance {
        public String value;
    }
    public static class TableFieldInstance extends FieldInstance {
        public List<List<TextFieldInstance>> value=new ArrayList<>();
    }
    public String examineField(List<Form.Field> fields){
        boolean right=true;
        String msg="";
        for (Form.Field field:fields){
            if (field instanceof Form.TextField){
                boolean res=examineTextField((Form.TextField) field);
                if (!res){
                    right=false;
                    msg="textField:%s not exist".formatted(field.label);
                    break;
                }
            }
            else if (field instanceof Form.TableField){
                String res=examineTableField((Form.TableField) field);
                if (!"right".equals(res)){
                    right=false;
                    msg="tableField:%s ,res:%s".formatted(field.label,res);
                    break;
                }
            }
        }
        if (right){
            return "right";
        }
        else {
            return msg;
        }
    }
    public boolean examineTextField(Form.TextField textField){
        for (FieldInstance fieldInstance:fieldInstances){
            if (fieldInstance instanceof TextFieldInstance && textField.label.equals(fieldInstance.name)){
                return true;
            }
        }
        return false;
    }
    public String examineTableField(Form.TableField tableField){
         boolean exist=false;
         String res="";
         boolean right=true;
        for (FieldInstance fieldInstance:fieldInstances){
            if (fieldInstance instanceof TableFieldInstance && tableField.label.equals(fieldInstance.name)){
                exist=true;
                boolean rightOut=true;
                List<List<TextFieldInstance>> value=((TableFieldInstance) fieldInstance).value;
                for (int i=0;i<value.size();i++){
                    boolean rightIn=true;
                    List<TextFieldInstance> column=value.get(i);
                    for (int j=0;j<column.size();j++){
                        TextFieldInstance textFieldInstance=column.get(j);
                        Form.TextField textField=tableField.children.get(j);
                        if (!textFieldInstance.name.equals(textField.label)){
                            res="i:%d,j:%d,name:%s,label:%s not match".formatted(i,j,textFieldInstance.name,textField.label);
                            rightIn=false;
                            break;
                        }
                    }
                    if (!rightIn){
                        rightOut=false;
                        break;
                    }
                }
                if (!rightOut){
                    right=false;
                    break;
                }
            }
        }
        if (!exist) {
            return "not exist";
        }
        else if (!right){
            return res;
        }
        else {
            return "right";
        }

    }
}

//    public  boolean repeated(){
//        Set<String> labels=new HashSet<>();
//        for (Field field:fields){
//            if (labels.contains(field.label)){
//                return true;
//            }
//            else {
//                labels.add(field.label);
//            }
//        }
//        return false;
//    }